<template>
    <body>
        <div class="container">
            <div class="form-box" ref="formBox">
                <!-- 注册模块 -->
                <div class="register-box hidden">
                    <h1>注册</h1>
                    <input id="username" type="text" placeholder="用户名">
                    <input id="password" type="password" placeholder="密码">
                    <input id="again" type="password" placeholder="确认密码">
                    <button id="registerButton">注册</button>
                </div>
                <!-- 登录模块 -->
                <div class="login-box">
                    <h1>登录</h1>
                    <input id="username2" type="text" placeholder="用户名">
                    <input id="password2" type="password" placeholder="密码">
                    <button id="loginButton">登录</button>
                </div>
            </div>
            <div class="con-box left">
                <h2>欢迎注册</h2>
                <p>请确保两次密码一致哦～</p>
                <img src="../assets/注册图片.jpg" alt="">
                <p>已有账号</p>
                <button id="login">去登录</button>
            </div>
            <div class="con-box right">
                <h2>欢迎登录</h2>
                <p>请告诉我你的账号密码～</p>
                <img src="../assets/登录图片.jpg" alt="">
                <p>没有账号</p>
                <button id="register">去注册</button>
            </div>
        </div>
    </body>
</template>
  
<script>

//import Navbar from "../components/NavBar.vue";
import router from '@/router';
import $ from 'jquery'
//import { Stream } from 'stream';
//import { useStore } from 'vuex';
//import { ref } from 'vue'
//import store from '@/store';
export default {
    components: {
        /*setup() {
            const store = useStore();
            let username = ref('');
            let password = ref('');
            let error_message = ref('');

            const login = () => {
                error_message.value = "";
                store.dispatch("login", {

                })

            }
        }*/
    },
    methods: {
        refreshNavBar() {
            this.$root.$emit('refreshNavBar'); // 发送刷新事件
        }
    },
    mounted() {
        //const store = useStore();
        const login = document.getElementById('login');
        const register = document.getElementById('register');
        const formBox = this.$refs.formBox;
        const registerBox = formBox.getElementsByClassName('register-box')[0];
        const loginBox = formBox.getElementsByClassName('login-box')[0];

        register.addEventListener('click', () => {
            formBox.style.transform = 'translateX(80%)';
            loginBox.classList.add('hidden');
            registerBox.classList.remove('hidden');
        });

        login.addEventListener('click', () => {
            formBox.style.transform = 'translateX(0%)';
            registerBox.classList.add('hidden');
            loginBox.classList.remove('hidden');
        });

        const registerButton = document.getElementById('registerButton');
        registerButton.addEventListener('click', () => {
            const zh = document.getElementById('username');
            const mm = document.getElementById('password');
            const cf = document.getElementById('again');
            const name = zh.value;
            const pass = mm.value;
            const pass2 = cf.value;

            if (name === '' || pass === '') {
                alert('账号名和密码不能为空!');
            } else if (pass !== pass2) {
                alert('两次密码不相同，请检查');
            } else {
                let str1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-/=!@#$%^&*';
                let str2 = 'L73hz/sHgnKmQCD8YFfGj1toaR0S4JcTpi6rv5yw+BVbWPeNu2IlUOxEdqkM9XZ+A-*&^%$#@!';
                let encoded = '';
                for (let i = 0; i < pass.length; i++) { //简单的可逆加密，为了防止post时浏览器认为密码泄露，后端还会使用hash方法进行严格的加密
                    let k = str1.indexOf(pass[i]);
                    if (k == -1) {
                        alert("密码只能包含大小写英文字母、数字和'!@#$%^&*+-/='这些特殊字符");
                        return;
                    }
                    encoded += str2[k];
                }
                $.ajax({
                    url: "http://10.142.225.1:3000/user/account/register",
                    type: "post",
                    data: {
                        username: name,
                        password: encoded,
                        confirmedPassword: encoded
                    },
                    success(resp) {
                        if (resp.message == "Success!")
                            alert('注册成功!');
                        else alert(resp.message);
                    },
                    error() { }
                })/*
                $.ajax({
                    url: "http://10.142.225.1:3000/user/add/" + name + "/" + pass,
                    type: "get",
                    success(resp) {
                        if (resp == "Success!")
                            alert('注册成功!');
                        else alert(resp);
                    },
                    error: function (xhr, _status, error) {
                        console.log("AJAX 请求失败");
                        console.log("状态码：" + xhr.status);
                        console.log("错误信息：" + error);
                    }
                })
                */
            }
        });

        const zh = document.getElementById('username2');
        const mm = document.getElementById('password2');
        const loginButton = document.getElementById('loginButton');
        loginButton.addEventListener('click', () => {
            const name = zh.value;
            const pass = mm.value;
            let str1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-/=!@#$%^&*';
            let str2 = 'L73hz/sHgnKmQCD8YFfGj1toaR0S4JcTpi6rv5yw+BVbWPeNu2IlUOxEdqkM9XZ+A-*&^%$#@!';
            let encoded = '';
            for (let i = 0; i < pass.length; i++) { //简单的可逆加密，为了防止post时浏览器认为密码泄露，后端还会使用hash方法进行严格的加密
                let k = str1.indexOf(pass[i]);
                if (k == -1) {
                    alert("密码只能包含大小写英文字母、数字和'!@#$%^&*+-/='这些特殊字符");
                    return;
                }
                encoded += str2[k];
            }
            if (name === '' || pass === '') {
                alert('账号名和密码不能为空！');
            } else /*if (localStorage.getItem(name) === null) {
                alert('账号未注册!');
            } else if (localStorage.getItem(name) !== pass) {
                alert('密码错误!');
            } else {
                localStorage.setItem('now', '');
                alert("登陆成功!");
            }*/
                $.ajax({
                    url: "http://10.142.225.1:3000/user/account/token",
                    type: "post",
                    data: {
                        username: name,
                        password: encoded
                    },
                    success(resp) {
                        if (resp.message == "Success!") {
                            //store.commit("upd145ateUser", state, resp);
                            //context.commit("updateToken", resp.token);
                            localStorage.setItem("is_login", "true");
                            console.log(localStorage.getItem("is_login"));
                            localStorage.setItem("login_user", name);
                            console.log(localStorage.getItem("login_user"));
                            localStorage.setItem("token", resp.token);
                            localStorage.setItem("purview", resp.purview);
                            //eventBus.$emit('refreshNav');
                            //location.reload();
                            router.push({ name: "home" });
                            //Navbar.methods.refresh();
                            //alert('登陆成功!');
                            //跳转
                        }
                        else alert(resp.message);
                    },
                    error() {
                        alert("账号名或密码错误!");
                    }
                })
        });
    },
};

</script>
  
<style scoped>
* {
    /* 初始化 */
    margin: 0;
    padding: 0;
}

body {
    /* 100%窗口高度 */
    height: 100vh;
    /* 水平+垂直居中 */
    display: flex;
    justify-content: center;
    align-items: center;
    /* 设置背景 */
    /*纯色背景*/
    /*background: linear-gradient(200deg, #e7f3f3, #e3eeff);*/
    /*图片背景*/
    background-image: url(../assets/背景3.png);
    background-size: cover;
    /* 将背景图片按比例缩放，以覆盖整个背景区域 */
    background-repeat: no-repeat;
    /* 禁止背景图片重复 */
    background-position: center center;
    /* 将背景图片水平垂直居中 */
}

.container {
    background-color: lightcyan;
    width: 650px;
    height: 415px;
    border-radius: 5px;
    /* 阴影 */
    box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.1);
    /* 相对定位 */
    position: relative;
}

/*主体框*/
.form-box {
    /* 绝对定位 */
    position: absolute;
    top: -10%;
    left: 5%;
    background-color: #46c1d3;
    width: 320px;
    height: 500px;
    border-radius: 5px;
    box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2;
    /* 动画过渡 加速后减速 */
    transition: 0.5s ease-in-out;
}

.register-box,
.login-box {
    /* 弹性布局 垂直排列 */
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
}

.hidden {
    display: none;
    transition: 0.5s;
}

/*标题*/
h1 {
    text-align: center;
    margin-bottom: 25px;
    /* 大写 */
    text-transform: uppercase;
    color: #fff;
    /* 字间距 */
    letter-spacing: 5px;
}

input {
    background-color: transparent;
    width: 70%;
    color: #fff;
    border: none;
    /* 下边框样式 */
    border-bottom: 1px solid rgba(255, 255, 255, 0.4);
    padding: 10px 0;
    text-indent: 10px;
    margin: 8px 0;
    font-size: 14px;
    letter-spacing: 2px;
}

/*输入框*/
input::placeholder {
    color: #fff;
}

input:focus {
    color: #0a47b0;
    outline: none;
    border-bottom: 1px solid #1255c880;
    transition: 0.5s;
}

input:focus::placeholder {
    opacity: 0;
}

/* 登录、注册按钮格式 */
.form-box button {
    width: 70%;
    margin-top: 35px;
    background-color: #f6f6f6;
    outline: none;
    border-radius: 8px;
    padding: 13px;
    color: #2460c0;
    letter-spacing: 2px;
    border: none;
    cursor: pointer;
}

.form-box button:hover {
    background-color: #2460c0;
    color: #f6f6f6;
    transition: background-color 0.5s ease;
}

.con-box {
    width: 50%;
    /* 弹性布局 垂直排列 居中 */
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    /* 绝对定位 居中 */
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
}

.con-box.left {
    left: -2%;
}

.con-box.right {
    right: -2%;
}

/*图片上方小标题格式*/
.con-box h2 {
    color: #4183cf;
    font-size: 25px;
    font-weight: bold;
    letter-spacing: 3px;
    text-align: center;
    margin-bottom: 4px;
}

/*图片上方文字格式*/
.con-box p {
    font-size: 12px;
    letter-spacing: 2px;
    color: #689af0;
    text-align: center;
}

/* 此处为照片格式 */
.con-box img {
    width: 200px;
    height: 200px;
    opacity: 0.9;
    margin: 20px 0;
    border-radius: 8px;
    /*圆角*/
}

/* 此处为去登陆、注册按键格式 */
.con-box button {
    margin-top: 3%;
    background-color: #fff;
    color: #3184cc;
    border: 1px solid #50b3ec;
    padding: 6px 10px;
    border-radius: 5px;
    letter-spacing: 1px;
    outline: none;
    cursor: pointer;
}

.con-box button:hover {
    background-color: #50b3ec;
    color: #fff;
    transition: background-color 0.5s ease;
}
</style>